/**
 * 
 */
package com.gmail.blackdog1987.onedollar.web.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gmail.blackdog1987.onedollar.service.UserService;

/**
 * 
 * @author 董国超
 *
 */
@Controller
public class UserController extends BaseController {
	@Autowired
	private UserService userService;

	/**
	 * 获取推荐人
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getOldPerson", method=RequestMethod.POST)
	public Map<String, Object> getOldPerson(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.getOldPerson(filter);
		return decorateResult(result);
	}

	/**
	 * 设置推荐人
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="setOldPerson", method=RequestMethod.POST)
	public Map<String, Object> setOldPerson(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.createOldPerson(filter);
		return decorateResult(result);
	}

	/**
	 * 登录
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="login", method=RequestMethod.POST)
	public Map<String, Object> login(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.doLogin(filter);
		return decorateResult(result);
	}

	/**
	 * 注册
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="reg", method=RequestMethod.POST)
	public Map<String, Object> reg(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.doReg(filter);
		return decorateResult(result);
	}

	/**
	 * 忘记密码
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="forgetAccount", method=RequestMethod.POST)
	public Map<String, Object> forgetAccount(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.doForgetAccount(filter);
		return decorateResult(result);
	}

	/**
	 * 修改用户信息
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="updateInfo", method=RequestMethod.POST)
	public Map<String, Object> updateInfo(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.updateInfo(filter);
		return decorateResult(result);
	}

	/**
	 * 获取用户信息
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getMyInfo", method=RequestMethod.POST)
	public Map<String, Object> getuser(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.getInfo(filter);
		return decorateResult(result);
	}

	/**
	 * 获取验证码
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getcode", method=RequestMethod.POST)
	public Map<String, Object> getcode(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.doGetCode(filter);
		return decorateResult(result);
	}

	/**
	 * 获取我的地址列表
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getMyAddress", method=RequestMethod.POST)
	public Map<String, Object> getMyAddress(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.getAddressList(filter);
		return decorateResult(result);
	}

	/**
	 * 新增地址
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addAddress", method=RequestMethod.POST)
	public Map<String, Object> addAddress(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.doAddAddress(filter);
		return decorateResult(result);
	}

	/**
	 * 修改地址
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="updateAddress", method=RequestMethod.POST)
	public Map<String, Object> updateAddress(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.updateAddress(filter);
		return decorateResult(result);
	}

	/**
	 * 删除我的地址
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="deleteAddress", method=RequestMethod.POST)
	public Map<String, Object> deleteAddress(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.deleteAddress(filter);
		return decorateResult(result);
	}

	/**
	 * 设置默认地址
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="setDefaultAddress", method=RequestMethod.POST)
	public Map<String, Object> setDefaultAddress(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.updateDefaultAddess(filter);
		return decorateResult(result);
	}

	/**
	 * 提现
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="withDraw", method=RequestMethod.POST)
	public Map<String, Object> withDraw(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.doWithDraw(filter);
		result.put("time", System.currentTimeMillis());
		return result;
	}

	/**
	 * 获取我的账单
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	 @RequestMapping(value="accountHistory", method=RequestMethod.POST)
	 public Map<String, Object> accountHistory(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.getChargeList(filter);
		return decorateResult(result);
	}

	/**
	 * 获取银行卡
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="getBankCard", method=RequestMethod.POST)
	public Map<String, Object> getBankCard(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.getBankCard(filter);
		return decorateResult(result);
	}

	/**
	 * 新增银行卡
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addBankCard", method=RequestMethod.POST)
	public Map<String, Object> addBankCard(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.createBankCard(filter);
		return decorateResult(result);
	}

	/**
	 * 删除银行卡
	 * @param filter
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delBankCard", method=RequestMethod.POST)
	public Map<String, Object> delBankCard(@RequestParam Map<String, String> filter, HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> result = userService.deleteBankCard(filter);
		return decorateResult(result);
	}

	@Override
	protected Map<String, Object> decorateResult(Map<String, Object> result) {
		result.put("time", System.currentTimeMillis());
		return result;
	}
}
